package top.dark.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.dark.product.entity.AttrEntity;
import top.dark.product.entity.AttrGroupEntity;
import top.dark.product.service.AttrAttrgroupRelationService;
import top.dark.product.service.AttrGroupService;
import top.dark.common.utils.PageUtils;
import top.dark.common.utils.R;
import top.dark.product.service.AttrService;
import top.dark.product.service.CategoryService;
import top.dark.product.vo.AttrGroupRelationVo;
import top.dark.product.vo.AttrGroupRespVo;


/**
 * 属性分组
 *
 * @author lijiao
 * @email DarknessShadow11@outlook.com
 * @date 2021-02-01 10:42:11
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 获取分类下所有分组&关联属性
     */
    @GetMapping("/{catelogId}/withattr")
    public R getGroupAndAttrByCatelogId(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupRespVo> data = attrGroupService.getGroupAndAttrByCatelogId(catelogId);
        return R.ok().put("data", data);
    }

    /**
     * 添加属性与分组关联关系
     * /product/attrgroup/attr/relation
     */
    @RequestMapping("/attr/relation")
    public R addAttrRelation(@RequestBody List<AttrGroupRelationVo> vos) {
        attrAttrgroupRelationService.addAttrRelation(vos);
        return R.ok();
    }

    /**
     * 获取属性分组没有关联的其他属性
     *  /product/attrgroup/{attrgroupId}/noattr/relation
     */
    @RequestMapping("/{attrgroupId}/noattr/relation")
    public R getNoattrRelation(@PathVariable("attrgroupId")Long attrgroupId,
                               @RequestParam Map<String, Object> params ){
        PageUtils page = attrService.getNoattrRelation(attrgroupId, params);
        return R.ok().put("page", page);
    }

    /**
     * 删除属性与分组的关联关系
     */
    @RequestMapping("/attr/relation/delete")
    public R relationDelete(@RequestBody AttrGroupRelationVo[] vos) {
        attrService.batchDeleteRelation(vos);
        return R.ok();
    }

    /**
     * 获取属性分组的关联的所有属性
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> attrEntities = attrService.attrRelation(attrgroupId);
        return R.ok().put("data", attrEntities);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{categoryId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("categoryId") Long categoryId){
        // PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, categoryId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] catelogPath = categoryService.getCatelogPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
